using System;
using System.Diagnostics;
using System.Net;
namespace Async_01 {

    public class Sync {
        private static readonly Stopwatch Watch = new Stopwatch ();

        public void StartSync () {

            Console.WriteLine ("----------------同步方法开始----------------");
            Watch.Start ();
            const string url1 = "http://www.cnblogs.com/";
            const string url2 = "http://www.cnblogs.com/liqingwen/";

            var result1 = CountCharacters (1, url1);
            var result2 = CountCharacters (2, url2);

            for (int i = 0; i < 3; i++) {
                ExtraOperation (i + 1);
            }

            Console.WriteLine ("url1的字符串个数为：{0}", result1);
            Console.WriteLine ("url2的字符串个数为：{0}", result2);

            Console.WriteLine ("---------------同步方法结束----------------");
            Console.ReadKey ();
        }
        private int CountCharacters (int id, string address) {
            var wc = new WebClient ();
            Console.WriteLine ("开始调用ID={0}:花费：{1} ms", id, Watch.ElapsedMilliseconds);
            var result = wc.DownloadString (address);
            Console.WriteLine ("调用完成ID={0}:花费：{1} ms", id, Watch.ElapsedMilliseconds);
            return result.Length;
        }
        private static void ExtraOperation (int id) {
            var s = "";
            for (int i = 0; i < 6000; i++) {
                s += i;
            }
            Console.WriteLine ("ID={0}的额外方法花费 :{1} ms", id, Watch.ElapsedMilliseconds);
        }
    }
}